package com.welsy.iter.business
{
	import com.welsy.iter.model.IterModelLocator;
	import com.welsy.iter.view.itercomponents.IterElementEditView;
	import com.welsy.iter.vo.iter.IterElementFactory;
	
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.net.*;
	
	import mx.containers.*;
	import mx.controls.Alert;
	import mx.controls.Tree;
	import mx.core.DragSource;
	import mx.core.IUIComponent;
	import mx.events.*;
	import mx.managers.DragManager;
	import mx.rpc.soap.*;
	
	public class DragAndDropUtils
	{

		public var alertConfirm:Boolean = true;
		
		public var cestinoPanel:Object;
		
		private var sovrapposizione:int = 0;  
		
		public function setCestinoPanel(myVBox:VBox):void {
        	cestinoPanel = Object(myVBox);
        }        
                    
        public function dragIt(event:MouseEvent):void {
        	if (IterModelLocator.getInstance().selectedIter.iter.editabled()) {
	        	var dragInitiator:IUIComponent = IUIComponent(event.currentTarget);
	            var ds:DragSource = new DragSource();         
	            DragManager.doDrag(dragInitiator, ds, event,null,0.0,0.0,0.5,true);
         	}
        }

        public function doDragEnter(event:DragEvent):void 
        {        	
        	var courseId:String = "0"
        	if (event.dragInitiator.name == "myTree"){
        		var selectedIterNode:XML=Tree(event.dragInitiator).selectedItem as XML
        		courseId = selectedIterNode.@courseId
        	}
        	
            var dropTarget:IUIComponent = IUIComponent(event.currentTarget);
            if ((((event.dragInitiator.name == "elemento") && (event.currentTarget.name == "area"))
            ||  ((event.dragInitiator.name == "blocco") && (event.currentTarget.name == "cestino"))
            ||  ((event.dragInitiator.name == "blocco") && (event.currentTarget.name == "blocco"))
            ||  ((event.dragInitiator.name == "blocco") && (event.currentTarget.name == "area"))
			||  (checkElementCourse(event.dragInitiator,event.currentTarget))              
				)){
	            if(event.dragInitiator == event.currentTarget){
		        	sovrapposizione = 1;
		        }
		        if(event.dragInitiator != event.currentTarget){
		        	sovrapposizione = 0;
		        }     
		             
				DragManager.acceptDragDrop(dropTarget);
            }
        }
        
        private function checkElementCourse(Initiator:IUIComponent,Target:Object):Boolean {
        	var returnValue:Boolean = false
        	        	
        	if (Initiator.name == "myTree"){
        		var courseId:String = "0"
        		var selectedIterNode:XML=Tree(Initiator).selectedItem as XML
        		courseId = selectedIterNode.@courseId        	

        		if ((Target.name == "area") && (courseId != "0"))
        		{
        			returnValue = true
        			var iterElementView:IterElementEditView;

        			for (var i:int=0; i<Target.getChildren().length; i++) {
        				iterElementView = IterElementEditView(Target.getChildAt(i))

        				if (iterElementView.elementIter.CourseId == courseId){        					
        					returnValue = false
        					break        					
        				}
        				else
        					returnValue = true
        			}
        		}
        	}
        	return returnValue
        }
        
        private function moduleProperties(event:MouseEvent):void {
        	Alert.show("click on the module hitarea (drag)");
        	trace(event.target); // true  
        	trace(event.currentTarget.parent.name);  		 
        }
        
        public function doDragDrop(event:DragEvent):void {
        	if(sovrapposizione == 0){
        		if (event.dragInitiator.name =="elemento" || event.dragInitiator.name =="myTree") {		        	
		        	var iterElementView:IterElementEditView = new IterElementEditView
		        	switch (event.dragInitiator.name) {
		        		case "elemento":
							iterElementView.elementIter = IterElementFactory.getIterElementByEvent(event)
		        			break;
		        		case "myTree":
		        			var selectedCourseNode:XML=Tree(event.dragInitiator).selectedItem as XML
							iterElementView.elementIter = IterElementFactory.getIterElementByXml(selectedCourseNode)            		
		        			break;
		        	}					
					
		            iterElementView.addEventListener(DragEvent.DRAG_ENTER,doDragEnter);
		            iterElementView.addEventListener(DragEvent.DRAG_DROP,doDragDrop);
		            iterElementView.addEventListener(MouseEvent.MOUSE_MOVE, dragIt);
		      		
		            event.currentTarget.addChild(iterElementView);	
		        }
		        if (event.dragInitiator.name == "blocco"){
	            	if (event.currentTarget.name == "cestino"){
		           			event.dragInitiator.width = 0;
		        			event.dragInitiator.height = 0;
		        			event.dragInitiator.alpha = 0;
		        			event.currentTarget.addChild(event.dragInitiator);
		        			event.currentTarget.removeChildAt(0);
		            }
		            else {
	        			event.currentTarget.addChild(event.dragInitiator);
	        		}	               
		        }		                
		        if (event.dragInitiator.name == "blocco"){
		           	if (event.currentTarget.name == "blocco"){         		
		           		var j:int = event.currentTarget.parent.getChildIndex(event.currentTarget);
		           		var pt2:Point = new Point(event.localX, event.localY);
	          			pt2 = event.currentTarget.localToGlobal(pt2);
		           		pt2 = event.currentTarget.globalToContent(pt2);
		           		var posizioneMouse2: int = pt2.x;
		           		var w:int = 0;
		           		
		           		if ( posizioneMouse2 < event.currentTarget.height/2){
		           			w = 0;
		           		}
		           		
		           		if ( posizioneMouse2 >= event.currentTarget.height/2){
		           			w = 1
		           		}
		           		
		           		if (j <= event.currentTarget.parent.numChildren-1) {
		            		event.currentTarget.parent.addChildAt(event.dragInitiator,j+w);
		            	}
		            }
		        }
        	}
        	else
        		Alert.show("Non è possibile inserire un elemento all'interno di se stesso");
        }   
                  		
	}
}